//
//  DHOrder_LeasedCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/17.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHOrder_LeasedCell.h"
#import "DHCartTableViewCell.h"
#import "DHOrderPayViewController.h"
#import "DHOrderDetail_CheckViewController.h"

@interface DHOrder_LeasedCell()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *topStateLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIButton *payButton;
@property (weak, nonatomic) IBOutlet UIButton *priceButton;
@property (weak, nonatomic) IBOutlet UIButton *stateButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeightLayout;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@property (strong, nonatomic) NSArray *contentProductArr;
@property (strong, nonatomic) NSDictionary *orderDic;

@end

@implementation DHOrder_LeasedCell

- (NSArray *)contentProductArr{
    if (!_contentProductArr) {
        _contentProductArr = [NSArray array];
    }
    return _contentProductArr;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"DHCartTableViewCell" bundle:nil] forCellReuseIdentifier:kDHCartTableViewCell];
    
    [self.payButton bk_addEventHandler:^(UIButton *sender) {
        if ([sender.titleLabel.text isEqualToString:@"立即支付"]) {
            NSMutableArray *productIDArr = [NSMutableArray array];
            [self.contentProductArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSDictionary *size = [[obj objectForKey:@"sizes"] objectAtIndex:0];;
                [productIDArr addObject:@{@"_id":size[@"_id"]}];
            }];
            
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            [params setObject:@{@"leased":productIDArr} forKey:@"products"];
            if ([self.orderDic objectForKey:@"consignee"]) {
                [params setObject:[self.orderDic objectForKey:@"consignee"] forKey:@"consignee"];
            }
            DHOrderPayViewController *orderPayController = [[DHOrderPayViewController alloc] initWithOrders:@[self.orderDic] orderValidParams:params];
            [self.viewController.navigationController pushViewController:orderPayController animated:YES];
        }
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)updateCell:(NSDictionary *)orderDic{
    self.orderDic = orderDic;
    self.bottomView.hidden = NO;
    
    self.contentProductArr = [orderDic objectForKey:@"content"];
    
    self.titleLabel.text = [orderDic objectForKey:@"type"];
    if ([[orderDic objectForKey:@"status"] integerValue] == OrderStateUnpaid) {
        
        self.topStateLabel.text = @"待支付";

        self.priceButton.hidden = NO;
        self.stateButton.hidden = NO;
        self.payButton.hidden = NO;

        __block CGFloat totalPrice = 0;
        NSArray *costs = [orderDic objectForKey:@"costs"];
        [costs enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CGFloat cost_value = [[obj objectForKey:@"cost_value"] floatValue];
            if (cost_value >= 0) {
                totalPrice += cost_value;
            }else{
                totalPrice -= cost_value;
            }
        }];
        [self.priceButton setTitle:[NSString stringWithFormat:@"订单金额：￥%.2f", totalPrice] forState:UIControlStateNormal];
        [self.stateButton setTitle:@"支付状态：未完成支付" forState:UIControlStateNormal];
        
        
    }else if ([[orderDic objectForKey:@"status"] integerValue] == OrderStateUnusual) {
        
        self.topStateLabel.text = @"异常";
        
        self.priceButton.hidden = YES;
        self.stateButton.hidden = YES;
        self.payButton.hidden = YES;
        
    }else if ([[orderDic objectForKey:@"status"] integerValue] == OrderStateUnreceiving) {
        
        self.topStateLabel.text = @"待收货";
        
        self.priceButton.hidden = NO;
        self.stateButton.hidden = NO;
        self.payButton.hidden = YES;
        NSDate *date = [NSDate dateWithServerISOString:[self.orderDic objectForKey:@"createdAt"] format:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
        [self.priceButton setTitle:[NSString stringWithFormat:@"创建时间：%@",[NSDate stringFromDate:date withFormat:@"yyyy/MM/dd HH:mm"]] forState:UIControlStateNormal];
        [self.stateButton setTitle:[NSString stringWithFormat:@"物流状态：%@",[[self.orderDic objectForKey:@"consignee"] objectForKey:@"logistic_status"]] forState:UIControlStateNormal];
        
    }else if ([[orderDic objectForKey:@"status"] integerValue] == OrderStateUnshipments) {
        
        self.topStateLabel.text = @"待发货";
        
        self.priceButton.hidden = NO;
        self.stateButton.hidden = NO;
        self.payButton.hidden = YES;
        NSDate *date = [NSDate dateWithServerISOString:[self.orderDic objectForKey:@"createdAt"] format:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
        [self.priceButton setTitle:[NSString stringWithFormat:@"创建时间：%@",[NSDate stringFromDate:date withFormat:@"yyyy/MM/dd HH:mm"]] forState:UIControlStateNormal];
        [self.stateButton setTitle:@"物流状态：等待发货" forState:UIControlStateNormal];
        
    }else if ([[orderDic objectForKey:@"status"] integerValue] == OrderStateCompleted) {
        
        self.topStateLabel.text = @"已完成";
        
        self.priceButton.hidden = NO;
        self.stateButton.hidden = NO;
        self.payButton.hidden = YES;
        NSDate *date = [NSDate dateWithServerISOString:[self.orderDic objectForKey:@"createdAt"] format:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
        [self.priceButton setTitle:[NSString stringWithFormat:@"创建时间：%@",[NSDate stringFromDate:date withFormat:@"yyyy/MM/dd HH:mm"]] forState:UIControlStateNormal];
        [self.stateButton setTitle:[NSString stringWithFormat:@"物流状态：%@",[[self.orderDic objectForKey:@"consignee"] objectForKey:@"logistic_status"]] forState:UIControlStateNormal];
        
    }else if ([[orderDic objectForKey:@"status"] integerValue] == OrderStateCancel) {
        
        self.topStateLabel.text = @"已取消";
        
        self.priceButton.hidden = NO;
        self.stateButton.hidden = NO;
        self.payButton.hidden = YES;
        NSDate *date = [NSDate dateWithServerISOString:[self.orderDic objectForKey:@"createdAt"] format:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
        [self.priceButton setTitle:[NSString stringWithFormat:@"创建时间：%@",[NSDate stringFromDate:date withFormat:@"yyyy/MM/dd HH:mm"]] forState:UIControlStateNormal];
        [self.stateButton setTitle:@"超过30分钟未支付，订单已被关闭" forState:UIControlStateNormal];
        
    }
    [self.tableView reloadData];
    self.tableViewHeightLayout.constant = 100 * [[orderDic objectForKey:@"content"] count];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.contentProductArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHCartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHCartTableViewCell forIndexPath:indexPath];
    cell.priceLabel.hidden = YES;
    cell.selectButton.hidden = YES;
    NSDictionary *product = [self.contentProductArr objectAtIndex:indexPath.row];
    [cell updateCellFromOrderManager:product];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DHOrderDetail_CheckViewController *odController = [[DHOrderDetail_CheckViewController alloc] init];
    odController.orderID = [self.orderDic objectForKey:@"_id"];
    [self.viewController.navigationController pushViewController:odController animated:YES];
}

@end
